package com.tadpole.music.view.widget;

import android.app.Activity;
import android.view.View;
import android.view.animation.Animation;
import android.widget.TextView;
import androidx.viewpager.widget.ViewPager;
import com.tadpole.music.R;
import com.tadpole.music.adapter.base.NineGridViewVpAdapter;
import java.util.ArrayList;
import java.util.List;
import razerdp.basepopup.BasePopupWindow;

/**
 * 二维码弹窗
 * Created By 孙岩
 * on 2020/10/9
 */
public class BigImagePopup extends BasePopupWindow implements View.OnClickListener {
    private ViewPager viewPager;
    private TextView tvShowPicPosition;
    private List<String> mData = new ArrayList<>();
    private List<String> picTotalAddressOne;
    //大图下标
    private int picSubscript;
    private View viewBack;
    private Activity activity;
    public BigImagePopup(Activity activity,int picSubscript,List<String> picTotalAddressOne) {
        super(activity);
        this.activity = activity;
        viewBack = findViewById(R.id.viewBack);
       this.picSubscript = picSubscript;
       this.picTotalAddressOne = picTotalAddressOne;
        viewPager = (ViewPager) findViewById(R.id.vp);
        tvShowPicPosition= (TextView) findViewById(R.id.tv_show_pic_position);
        setVp();
    }

    @Override
    protected Animation initShowAnimation() {
        return null;
    }

    @Override
    public View getClickToDismissView() {
        return getPopupWindowView();
    }

    @Override
    public View onCreatePopupView() {
        return createPopupById(R.layout.activity_look_big_pic);
    }

    @Override
    public View initAnimaView() {
        return findViewById(R.id.rlCode);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.viewBack) {
            dismiss();
        }
    }

    private void setVp() {
        if(picTotalAddressOne!=null&&picTotalAddressOne.size()>0){
            for(int i=0;i<picTotalAddressOne.size();i++){
                mData.add(picTotalAddressOne.get(i));
            }
        }

        viewPager.setAdapter(new NineGridViewVpAdapter(activity, mData, new NineGridViewVpAdapter.NineGridViewItemCLickListener() {
            @Override
            public void ItemClick() {
                dismiss();
            }

            @Override
            public void ItemLongClick(String itemPicAddress) {
            }
        },false,false));
        if(picSubscript!=-1){
            viewPager.setCurrentItem(picSubscript);
            picSubscript=picSubscript+1;
            tvShowPicPosition.setText(""+picSubscript+"/" + mData.size());
        }else {
            tvShowPicPosition.setText("1/" + mData.size());
        }
        viewPager.setOffscreenPageLimit(2);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                tvShowPicPosition.setText((position+1)+"/"+mData.size());
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

    }
}
